package org.youchan.aco.p2p;

import org.youchan.util.Message;
import org.youchan.util.MessageHandler;
import org.youchan.util.MessagingQueue;

public class SessionManager implements MessageHandler<Message<SessionCommand>> {
	
	private static SessionManager _self;
	
	private MessagingQueue<Message<SessionCommand>> queue;
	A2AManager a2a;
	
	private SessionManager() {
		a2a = A2AManager.getInstance();
		queue = new MessagingQueue<Message<SessionCommand>>(this);
		queue.start();
	}
	
	public static SessionManager getInstance() {
		if (_self == null) {
			_self = new SessionManager();
		}
		return _self;
	}

	public void sendMessage(Message<SessionCommand> message) {
		queue.sendMessage(message);
	}
	
	public void handle(Message<SessionCommand> message) {
		SessionCommand command = message.getCommand();
		switch (message.getCommand().status) {
		case SEND:
			String[] memberId = command.getArray("target");
			a2a.sendMessage(command.outputXML(), memberId);
			break;
		case RECEIVED:
			System.out.println(command.outputXML());
			break;
		}
	}

}
